<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="用工企业名称" prop="initiateCompanyName" label-width="100px">
        <el-input
          v-model="queryParams.initiateCompanyName"
          placeholder="请输入用工企业名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="税源地名称" prop="acceptCompanyName">
        <el-input
          v-model="queryParams.acceptCompanyName"
          placeholder="请输入税源地名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="auditStatus">
        <el-select v-model="queryParams.auditStatus" clearable placeholder="请选择审核状态">
          <el-option :value="0" label="代理审核中"></el-option>
          <el-option :value="4" label="待审核"></el-option>
          <el-option :value="1" label="审核通过"></el-option>
          <el-option :value="2" label="审核驳回"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['tax:cooperativeTax:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cooperativeTaxList" >
      <el-table-column label="用工企业名称" align="center" prop="initiateCompanyName" min-width="180">
        <template slot-scope="{ row }">
          <el-button @click="handleMerchant(row)" type="text" size="mini">{{row.initiateCompanyName}}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="税源地名称" align="center" prop="acceptCompanyName" min-width="180">
        <template slot-scope="{ row }">
          <el-button @click="handleTax(row)" type="text" size="mini">{{row.acceptCompanyName}}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="用工企业费率" align="center" prop="rate" width="130">
        <template slot-scope="{ row }">
          常规行业：{{math(row.conventionRate,row.rate)}}%
          <br />
          特殊行业：{{math(row.specialRate,row.rate)}}%
        </template>
      </el-table-column>
      <el-table-column label="代理费率" align="center" prop="rate" min-width="120">
        <template slot-scope="{ row }">
          常规行业：{{row.conventionRate}}%
          <br />
          特殊行业：{{row.specialRate}}%
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="rate" width="130">
        <template slot-scope="{ row }">
          <el-tag type="info" v-if="row.auditStatus === 0">代理审核中</el-tag>
          <el-tag type="info" v-else-if="row.auditStatus === 4">待审核</el-tag>
          <el-tag v-else-if="row.auditStatus === 1">审核通过</el-tag>
          <el-popover
            placement="bottom"
            title=""
            width="200"
            trigger="click"
            :content="row.reason">
            <el-tag slot="reference" v-if="row.auditStatus === 2" style="cursor: pointer" type="danger">点击查看驳回原因</el-tag>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="申请原因" align="center" prop="reason" width="130">
        <template slot-scope="{ row }">
          <el-popover
            placement="bottom"
            title=""
            width="200"
            trigger="click"
            :content="row.remarks">
            <el-tag slot="reference" style="cursor: pointer" type="success">点击查看申请原因</el-tag>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="申请时间" align="center" prop="createTime" width="180"/>
      <el-table-column label="审核时间" align="center" prop="auditTime" width="180" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.auditStatus === 4"
            icon="el-icon-more-outline"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['tax:cooperativeTax:audit']"
          >审核</el-button>

          <el-button
            size="mini"
            type="text"
            v-else-if="scope.row.auditStatus === 1"
            icon="el-icon-more-outline"
            @click="handleContract(scope.row)"
            v-hasPermi="['tax:cooperativeTax:audit']"
          >合同管理</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改合作税源对话框 -->
    <el-dialog :close-on-press-escape="false" :close-on-click-modal="false" title="审核用工企业" :visible.sync="open" width="500px" append-to-body destroy-on-close>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="审核状态" prop="auditStatus">
          <el-radio-group v-model="form.auditStatus">
            <el-radio
              v-for="dict in [{label:'审核成功',value:1},{label:'审核驳回',value:2}]"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="用工企业费率" prop="rate">
          <div style="gap: 5px;display: flex;align-items: center;">
            常规行业：{{math(detail.conventionRate, form.rate)}}%
            <br />
            特殊行业：{{math(detail.specialRate , form.rate)}}%
          </div>
        </el-form-item>

        <el-form-item label="申请原因" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" :autosize="{ minRows: 8, maxRows: 8}" disabled placeholder="请输入申请原因"></el-input>
        </el-form-item>
        <el-form-item label="驳回原因" prop="reason">
          <el-input v-model="form.reason" type="textarea" :autosize="{ minRows: 8, maxRows: 8}" placeholder="请输入驳回原因"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>

    <el-drawer :wrapperClosable="false" :close-on-press-escape="false" @close="taxData = {}" destroy-on-close title="税源地详情" :visible.sync="taxDetailOpen" size="55%" class="app-drawer" append-to-body>
      <tax-detail @cancel="handleCancel" :taxData="taxData"></tax-detail>
    </el-drawer>
    <el-drawer :wrapperClosable="false" :close-on-press-escape="false" @close="merchantData = {}" destroy-on-close title="用工企业详情" :visible.sync="merchantDetailOpen" size="55%" class="app-drawer" append-to-body>
      <merchant-detail @cancel="handleCancel" :merchantData="merchantData"></merchant-detail>
    </el-drawer>

    <el-drawer :wrapperClosable="false" :close-on-press-escape="false"  title="合同管理" :visible.sync="contractOpen" size="65%" destroy-on-close append-to-body>
      <contract-info v-if="contractOpen" :type="3" :organizationId="contractData.organizationId"></contract-info>
    </el-drawer>
  </div>
</template>


<script>
import { math } from "@/utils/ruoyi";
import ContractInfo from "@/views/contract/contractInfo/index.vue";
import TaxDetail from "@/views/tax/cooperativeTax/profile/tax_detail.vue";
import AgentDetail from "@/views/tax/cooperativeTax/profile/agent_detail.vue";
import MerchantDetail from "@/views/tax/cooperativeTax/profile/merchant_detail.vue";
import { listCooperativeTax, getCooperativeTax, auditCooperativeTax } from "@/api/tax/cooperativeTax"

export default {
  name: "tax",
  components: {MerchantDetail, ContractInfo, AgentDetail, TaxDetail},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合作税源表格数据
      cooperativeTaxList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      taxData:{},
      merchantData:{},
      detail:{},
      contractOpen:false,
      taxDetailOpen:false,
      merchantDetailOpen:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        auditStatus:null,
        acceptCompanyName: null,
        initiateCompanyName: null,
      },
      tax:{
        organizationId:null,
      },
      // 表单参数
      form: {
        reason:null,
        remarks:null,
        auditStatus:null,
        cooperativeId: null,
      },
      contractData:{
        organizationId:null,
      },
      // 表单校验
      rules: {
        auditStatus:[
          { required: true, message: "请选择审核状态", trigger: ["blur","change"] },
        ],
        rate: [
          { required: true, message: '请输入税源费率', trigger: 'blur' },
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    math,
    /** 查询合作税源列表 */
    getList() {
      this.loading = true
      listCooperativeTax(this.queryParams).then(response => {
        this.cooperativeTaxList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.detail = row
      const cooperativeId = row.cooperativeId
      getCooperativeTax(cooperativeId).then(response => {
        response.data.auditStatus = null
        this.form = response.data
        this.open = true
        this.title = "修改合作税源"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          auditCooperativeTax(this.form).then(response => {
            this.$modal.msgSuccess("修改成功")
            this.open = false
            this.getList()
          })
        }
      })
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('tax/cooperativeTax/export', {
        ...this.queryParams
      }, `cooperativeTax_${new Date().getTime()}.xlsx`)
    },
    handleTax(row){
      this.taxData = row
      this.taxDetailOpen = true
    },
    handleMerchant(row){
      this.merchantData = row
      this.merchantDetailOpen = true
    },
    handleCancel(row){
      if (row){
        this.getList();
      }
      this.taxDetailOpen = false
      this.merchantDetailOpen = false
    },
    handleContract(row){
      this.contractData.organizationId = row.initiateOrganizationId
      this.contractOpen = true
    }
  }
}
</script>

<style scoped>

.wen{
  width: 18px;
  height: 18px;
  display: flex;
  color: #ffffff;
  cursor: pointer;
  border-radius: 50%;
  align-items: center;
  justify-content: center;
  background-color: #E6A23C;
}
</style>
